<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\adminapi\controller;


use crmeb\services\CacheService;
use think\Response;
use app\services\other\UploadService;
use app\Request;
use app\services\system\attachment\SystemAttachmentServices;
use think\facade\Cache;
use CURLFile;
class PublicController
{

    /**
     * 下载文件
     * @param string $key
     * @return Response|\think\response\File
     */
    public function download(string $key = '')
    {
        if (!$key) {
            return Response::create()->code(500);
        }
        $fileName = CacheService::get($key);
        if (is_array($fileName) && isset($fileName['path']) && isset($fileName['fileName']) && $fileName['path'] && $fileName['fileName'] && file_exists($fileName['path'])) {
            CacheService::delete($key);
            return download($fileName['path'], $fileName['fileName']);
        }
        return Response::create()->code(500);
    }

    /**
     * 获取workerman请求域名
     * @return mixed
     */
    public function getWorkerManUrl()
    {
        return app('json')->success(getWorkerManUrl());
    }

    /**
     * 文件上传
     * @param Request $request
     * @param SystemAttachmentServices $services
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function upload_file($data)
    {
        $request = Request();
        $data2 = input('ifa','0');
        $manager_id = 1;
        $uid = 'car_'.$manager_id;

        $upload = UploadService::init(null,$data2);
        $info = $upload->to('car')->validate()->setAuthThumb(false)->move2($data);
        if ($info === false) {
            return app('json')->fail($upload->getError());
        }
        $res = $upload->getUploadInfo();
        $res2 = $res["dir"];
        $services = app()->make(SystemAttachmentServices::class);
        $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 3);
        if (Cache::has('start_uploads_' . $uid))
            $start_uploads = (int)Cache::get('start_uploads_' . $uid);
        else
            $start_uploads = 0;
        $start_uploads++;
        Cache::set('start_uploads_' . $uid, $start_uploads, 86400);
        $res['dir'] = path_to_url($res['dir']);
        if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
        $res['status']=2;
        if($res['dir'])$res['status']=1;
        return $res;
    }
}
